<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .row {
            display: flex;
        }

        .add {
            border: 1px solid red;
            margin-left: 30px;
        }

        .title {
            display: flex;
            width: 600px;
            justify-content: space-between;
        }

        .body {
            display: flex;
            display: flex;
            width: 600px;
            justify-content: space-between;
        }
    </style>
</head>

<body>
    <script src="../框架相关的js/vue.js"></script>
    <div id="app">
        <!-- <div>{{sum}}</div> -->

        <!-- <div class="row" v-for="(item,index) in arr" ::key="index">
             <div>
              <input type="checkbox" v-model="item.status">

             </div>
             <div>
               <span>商品名称: {{item.name}}</span>

             </div>
             <div>
               <span>商品单价: {{item.price}}</span>

             </div>
                <div class="add">
                    <button>-</button>
                    <span>1</span>
                    <button>+</button>
                </div>
                <div>
                    单间商品合计 1999
                </div>
         </div> -->

        <div>
            <div class="title">
                <div>全选：<input @click="selectall" type="checkbox" ref="all"></div>
                <div>商品名称</div>
                <div>商品单价</div>
                <div>商品件数</div>
                <div>小计</div>
            </div>
            <div class="body" v-for="(item,index) in arr" ::key="index">

                <div>
                    <input @click="selectone" type="checkbox" v-model="item.status">

                </div>
                <div>
                    <span> {{item.name}}</span>

                </div>
                <div>
                    <span>{{item.price}}</span>

                </div>
                <div class="add">
                    <button @click="jian(item)">-</button>
                    <span>{{item.num}}</span>
                    <button @click="add(item)">+</button>
                </div>
                <div>
                    {{item.num*item.price}}
                </div>
            </div>

            <div> 合计 <span>总件数：{{totalnum}}</span> <span>总价格：{{totalprice}}</span> </div>
        </div>

    </div>




    <script>
        new Vue({
            el: '#app',

            data: {
                all: false,
                num1: 10,
                num2: 20,
                arr: [{ name: '苹果', price: 50, status: false, num: 1 }, { name: '香蕉', price: 150, status: false, num: 1 }, { name: '西瓜', price: 70, status: false, num: 1 }, { name: '梨', price: 80, status: false, num: 1 }]
            },
            watch: {


                arr: {
                    handler(newVal, oldVal) {

                        this.$refs.all.checked = this.arr.every(item => {
                            return item.status
                        })

                    },
                    deep: true, // 加这个属性，深度监听
                },
            },
            computed: {

                sum() {
                    return this.num1 + this.num2
                },
                totalnum() {
                    let num = 0
                    this.arr.map(item => {
                        if (item.status) {
                            num += item.num
                        }
                    })
                    return num
                    console.log(item);
                },
                totalprice() {

                    let totalprice = 0
                    this.arr.map(item => {
                        if (item.status) {
                            totalprice += item.num * item.price
                        }
                    })
                    return totalprice

                }

            },
            methods: {
                selectone() {

                },
                selectall() {
                    console.log(this.$refs.all.checked);
                    console.log(this.all, '6666666');
                    this.arr.forEach(item => {

                        item.status = this.$refs.all.checked
                    });
                },
                jian(item) {
                    console.log(item, '减少件数');
                    if (item.num <= 1) {
                        item.num = 1
                        alert('不能再减少了')
                    }
                },
                add(item) {
                    console.log(item, '增加件数');
                    let num = item.num
                    num++
                    item.num = num
                    //    console.log(num,5555555);
                    //    this.$set(item,'num',num)
                    //    console.log(item,1111111);
                }
            },

            mounted() {


            },

        })
    </script>
</body>

</html>